package br.com.winesys.geta.editor;

import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import net.miginfocom.swing.MigLayout;
import br.com.winesys.geta.databinding.Field;

public abstract class AutomatedObjectEditor<T> extends AbstractObjectEditor<T> {

    private JPanel panel;

    public AutomatedObjectEditor() {
        super(new JPanel());
        this.configure();
    }

    protected final void configure() {
        this.panel = (JPanel) getComponent();
        this.panel.setLayout(new MigLayout());
        this.panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(3, 3, 3, 3)));
        for (Field field : this.getFields()) {
            if (field.getCaption() != null) {
                this.panel.add(new JLabel(field.getCaption()), field.getEditor().getClientProperty(Field.LABEL_LAYOUT));
            }
            if (field.getEditor() != null) {
                if (field.getEditor() instanceof JTextArea) {
                    this.panel.add((Component) field.getEditor().getClientProperty(Field.SCROLL), field.getEditor().getClientProperty(Field.LAYOUT));
                } else {
                    this.panel.add(field.getEditor(), field.getEditor().getClientProperty(Field.LAYOUT));
                }
            } else {
                this.panel.add(new JLabel(""));
            }
        }
    }
}
